package com.jz.community.moduleshopping.timeLimit.model;

import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.moduleshopping.timeLimit.bean.LimitGoodsBean;
import com.jz.community.moduleshopping.timeLimit.bean.LimitTimeBean;
import com.jz.community.moduleshopping.timeLimit.net.LimitNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class TimeLimitModelImp implements TimeLimitModel {
    private Context mContext;

    public TimeLimitModelImp(Context context) {
        this.mContext = context;
    }


    @Override
    public void initTimeData(String cityId, int page, int size, OnLoadListener<LimitTimeBean> onLoadListener) {
        Observable<LimitTimeBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, LimitNetApi.class)
                .initTimeData(cityId, page, size));
        new RxLoadingWrapper<LimitTimeBean>(mContext,false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });

    }

    @Override
    public void initGoodsData(String cityId, String startTime, String endTime, int page, int size, OnLoadListener<LimitGoodsBean> onLoadListener) {
        Observable<LimitGoodsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, LimitNetApi.class)
                .initGoodsData(cityId, startTime, endTime, page, size));
        new RxLoadingWrapper<LimitGoodsBean>(mContext,false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void sendTips(long gid, String startTime, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Observable<BaseResponseInfo> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, LimitNetApi.class)
                .sendTips(gid, startTime));
        new RxLoadingWrapper<BaseResponseInfo>(mContext,false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
